package org.jili.novoland.game;

import java.util.Vector;

import org.jili.novoland.R;
import org.jili.novoland.base.LayerManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ResourceManager {
    static private ResourceManager instance = null;
    private LayerManager layerManager;
    private Context context;
    private BattleMap battleFieldMap;
    private Vector<Person> personList= new Vector<Person>();
    private Person currentRole;

    public BattleMap getBattleFieldMap() {
        return battleFieldMap;
    }


    public  Person getCurrentRole(){
        return currentRole;
    }
    

    private ResourceManager(Context context) {
        this.context = context;
        layerManager = new LayerManager();
    }

    static public ResourceManager getInstance(Context context) {
        if (instance == null) {
            instance = new ResourceManager(context);
        }

        return instance;
    }

    public void load() {
        Person leadingRole = initLeadingRole();
        currentRole = leadingRole;
        personList.add(leadingRole);
        for(int i = 0; i < personList.size();i++){
            layerManager.append(personList.get(i));
        }
        battleFieldMap = initMap();
        layerManager.append(battleFieldMap);
        
    }

    public Bitmap getPng(int resId) {
        return BitmapFactory.decodeResource(context.getResources(), resId);
    }

    public Context getContext() {
        return context;
    }

    public LayerManager getLayerManager() {
        return layerManager;
    }

    private BattleMap initMap() {
        Bitmap mapPng = getPng(R.drawable.map);
        int mapMatrix[][] = {
                { 2, 2, 2, 2, 2, 1, 1, 3, 4, 1, 1, 1, 1, 2, 1, 2 },
                { 2, 1, 1, 1, 1, 1, 1, 3, 4, 4, 1, 1, 1, 2, 1, 2 },
                { 2, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 1, 1, 2, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 2 },
                { 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
                { 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
                { 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 4, 4, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 2 },
                { 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 2 },
                { 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 2 },
                { 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 2 } };
        int cols = mapMatrix[0].length;
        int rows = mapMatrix.length;

        BattleMap instance = new BattleMap(cols, rows, mapPng);
        instance.setPosition(0, 0);
        for (int x = 0; x < cols; x++) {
            for (int y = 0; y < rows; y++) {
                instance.setCell(x, y, mapMatrix[y][x]);
            }
        }
        return instance;
    }
    private Person initLeadingRole() {
        Bitmap rolePng = getPng(R.drawable.yuran);

        Person instance = new Person(rolePng); 
        instance.setPosition(56, 87);
        return instance;
    }

}
